package com.taotao.manager.service.impl;

import com.taotao.common.util.JsonUtils;
import com.taotao.manager.jedis.JedisClient;
import com.taotao.manager.mapper.TbItemDescMapper;
import com.taotao.manager.pojo.TbItemDesc;
import com.taotao.manager.service.ItemDescService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class ItemDescServiceImpl implements ItemDescService {
    @Autowired
    private TbItemDescMapper itemDescMapper;
    @Autowired
    private JedisClient jedis;
    @Value("${ITEM_INFO_KEY}")
    private String ITEM_INFO_KEY;
    @Value("${ITEM_INFO_KEY_EXPIRE}")
    private Integer ITEM_INFO_KEY_EXPIRE;

    @Override
    public TbItemDesc getItemDesc(Long itemId) {
        String key = ITEM_INFO_KEY + ":" + itemId + ":DESC";
        try {
            String jsonStr = jedis.get(key);
            if (!StringUtils.isEmpty(jsonStr)) {
                System.out.println("有缓存");
                jedis.expire( key, ITEM_INFO_KEY_EXPIRE);
                return JsonUtils.jsonToPojo(jsonStr, TbItemDesc.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
        try {
            jedis.set(key, JsonUtils.objectToJson(itemDesc));
        } catch (Exception e) {
            e.printStackTrace();
        }


        return itemDesc;
    }
}
